<template>
  <view class="studentList commonDiv">
    <view class="top">
      <view class="search-top">
        <view class="inner">
          <view class="inner-box">
            时间段：
            <u--input placeholder="请选择状态" v-model="searchForm.dateLabel" @focus="showPicker = true" style="width: 260rpx"></u--input>
          </view>
        </view>
        <view class="inner">
          <view class="inner-box">
            <!-- <u-button text="清空" style="width: 100px;margin: 0 10px;" @click="clearHandle"></u-button> -->
            <u-button type="primary" text="搜索" style="width: 100px;margin: 0 0 0 10px;"
              @click="searchHandle"></u-button>
          </view>
        </view>
      </view>
    </view>
    <view class="bot">
      <ua-table
        :columns="columnDefs"
        :data="data"
        :stripe="true"
        :border="true"
        :headerBold="true"
        headerBgColor="#f3f6f9"
        headerColor="#333"
        height="calc( 100vh - 30px)"
        style="border:1px solid #fff"
      >
        <template #default="{row, col, index}">
          <block v-if="col.slot == 'ifPermissionName'">
            <u-tag :text="row.ifPermissionName" size="small" :type="row.ifPermission === null ? 'info': row.ifPermission === '0' ? 'warning': row.ifPermission === '1' ? 'success' : row.ifPermission === '2' ? 'error' :''" style="padding:0 10px;"></u-tag>
          </block>
        </template>
      </ua-table>
      <view class="page-box">
        <page-pagination class="test-pagination" :numAround="true" :total="page.total"
          layout="page,prev,next,limit,total" :pageSizes="[20, 50, 100, 500]" :pageSize="page.limit"
          @sizeChange="sizeChange" color="#00796B" @pageChange="pageChange">
        </page-pagination>
      </view>
    </view>
    <view>
     <u-picker :show="showPicker" :columns="columns" keyName="label" confirmColor='#00796B' @cancel="showPicker = false"  @confirm="stateConfirm"></u-picker>
    </view>
    <quick-message ref="message"></quick-message>
  </view>
</template>
<script>
import mRequest from '@/api/index.js'
import hccmStore from '@/common/hccm-store.js'
export default {
  data () {
    return {
      showPicker: false,
      columns: [],
      // end
      searchForm: {
        dateValue: '',
        dateLabel: ''
      },
      page: {
        limit: 20,
        page: 1,
        total: 0
      },
      column:[
        { type:'index',  label: '序号', align: 'center', fixed:true, width:70 },
        { prop: 'studentName', label: '姓名', align: 'center', fixed:true, width: 140},
        { prop: 'stationsCount', label: '使用考站数', width:90, align: 'center'},
      ],
      columnDefs:[],
      data: []
    }
  },
  methods: {
    getDateList () {
      mRequest.recordsDateList({
        recordsId: hccmStore.getJudge()
      }).then(res => {
        if(res.data.records.length > 0){
          let aaabbb = res.data.records.sort((a, b) => parseInt(new Date(`${a.examStartDate}`).getTime()) - parseInt(new Date(`${b.examStartDate}`).getTime()))
          aaabbb.map(q => {
            q.label = `${q.examStartDate.substring(0, 16)} ~ ${q.examEndDate.substring(10, 16)}`
          })
          this.searchForm.dateValue = aaabbb[0].id
          this.searchForm.dateLabel = aaabbb[0].label
          this.columns = [aaabbb]
          this.getList()
        }
      })
    },
    sizeChange (val) {
      console.log(val)
      this.page.limit = val
      this.page.page = 1
      this.data = []
      this.getList()
    },
    pageChange(val){
      console.log(val)
      this.page.page = val
      this.data = []
      this.getList()
    },
    searchHandle () {
      this.page.page = 1
      this.data = []
      this.getList()
    },
    clearHandle () {
      this.page.page = 1
      this.searchForm = {}
      this.data = []
      this.getList()
    },
    // 判断对象几个属性
    countProperties (obj) {
      return Object.keys(obj).length
    },
    getList () {
      mRequest.examResultPageDate({
        recordsId: hccmStore.getJudge(),
        recordsDateId: this.searchForm.dateValue,
        limit: this.page.limit,
        page: this.page.page,
        orderSql: 'creat_date desc',
      }).then(res => {
        // this.data = res.page.records
        this.page.total = res.page.total
        let students = res.page.records
        // console.log(students, 222)
        let arr = []
        students.map(v => {
          let bbb = JSON.parse(JSON.stringify(v))
          bbb.stationsCount = this.countProperties(bbb.stations)
          let arrs = []
          // bbb.stations.map(qq => { })
          for (var el in bbb.stations) {
            arrs.push({
              name: el,
              teachers: bbb.stations[el]
            })
          }
          arrs = arrs.sort((a, b) => parseInt(a.name.substring(1, 2)) - parseInt(b.name.substring(1, 2)))
          bbb.stationsObj = arrs
          arr.push(bbb)
        })

        if (arr.length > 0) {
          let firstStudent = arr[0]
          let aaa = []
          firstStudent.stationsObj.map((q,k) => {
            let colorBakOut = ''
            if(k % 2 === 0){
              colorBakOut = 'rgba(243,229,219, 0.6)'
            }else if (k % 2 !== 0){
              colorBakOut = 'rgba(179,229,252, 0.6)'
            }
            q.teachers.map((p, index) => {
              let colorBakIn1 = ''
              let colorBakIn2 = ''
              // if( number % 2 === 0){
              //   colorBakIn1 = 'rgba(243,229,219, 0.3)'
              //   colorBakIn2 = 'rgba(243,229,219, 0.3)'
              // }else if(number % 2 !== 0){
              //   colorBakIn1 = 'rgba(179,229,252, 0.3)'
              //   colorBakIn2 = 'rgba(179,229,252, 0.3)'
              // }
              if(k % 2 === 0){
                colorBakIn1 = 'rgba(243,229,219, 0.3)'
                colorBakIn2 = 'rgba(243,229,219, 0.3)'
              }else if (k % 2 !== 0){
                colorBakIn1 = 'rgba(179,229,252, 0.3)'
                colorBakIn2 = 'rgba(179,229,252, 0.3)'
              }
              aaa.push({
                label: q.name + '考官' + Number(index + 1) + '姓名',
                prop: q.name + 'teacher' + Number(index + 1),
                align: 'center',
                width: 160,
                bgcolor:  colorBakIn1
              })
              aaa.push({
                label: q.name + '考官' + Number(index + 1) + '得分',
                prop: q.name + 'result' + Number(index + 1),
                align: 'center',
                width: 160,
                bgcolor: colorBakIn2
              })
            })
            aaa.push({
              label: q.name + '得分',
              prop: q.name + 'score',
              align: 'center',
              width: 120,
              bgcolor: colorBakOut
            })
          })
          aaa.push({
            label: '总得分',
            prop: 'allScore',
            align: 'center',
            width: 80,
            fixed: 'right',
            bgcolor: 'rgba(178,233,219,0.99)'
          })
          arr.map(pq => {
            let allScore = 0
            pq.stationsObj.map(q => {
              let score = 0
              let k = 0
              q.teachers.map((p) => {
                if (p.result >= 0) {
                  score += Number(p.result)
                }
                k++
              })
              score = (score / k).toFixed(2)
              q.teachers.map((p, index) => {
                // name
                let key1 = q.name + 'teacher' + Number(index + 1)
                // result
                let key2 = q.name + 'result' + Number(index + 1)
                // score
                let key3 = q.name + 'score'
                pq[key1] = p.teacherName || '暂无'
                pq[key2] = p.result < 0 ? '暂无' : p.result
                pq[key3] = score < 0 ? '暂无' : score
              })
              allScore += Number(score)
              pq.allScore = Number(allScore).toFixed(2)
              if (pq.allScore < 0) {
                pq.allScore = '暂无'
              }
            })
          })
          // 获取表头
          this.columnDefs = []
          this.columnDefs = this.column.concat(aaa)
          this.data = []
          this.data = JSON.parse(JSON.stringify(arr))
          console.log(this.data)
          this.page.total = res.page.total
        } else {
          this.columnDefs = this.column.concat([])
          this.data = []
          this.data = JSON.parse(JSON.stringify(arr))
          console.log(this.data)
          this.page.total = 0
        }
      })
    },
    pullUpLoadingAction () {
      if (this.page.total <= this.page.limit * this.page.page) {
        return
      } else {
        this.page.page++
        this.getList()
      }
    },
    stateConfirm(val){
      this.searchForm.dateLabel = val.value[0].label
      this.searchForm.dateValue = val.value[0].id
      this.showPicker = false
    }
  },
  onLoad (options) {
    // this.getList()
    this.getDateList()
  },
  watch:{
  }
}
</script>
<style lang='scss'>
.studentList {
  display: flex;
  height: calc( 100vh - 45px );
  flex-direction: column;
  padding: 0px 20px 20px;
  box-sizing: border-box;
  background: #f5f5f5;
  .top {
    height: auto;
    width: 100%;
    .search-top {
      font-size: 14px;
      padding: 10px;
      border-radius: 8px 8px 0 0;
      // overflow-x: auto;
      flex-wrap: wrap;
      display: flex;
      background: #fff;
      .inner {
        flex: 0 0 200px;
        /* 确保每个子元素有固定宽度 */
        display: inline-block;
        padding-right: 20px;
        padding-bottom: 10px;

        .inner-box {
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }

    .search-bot {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      background: #fff !important;
    }
  }

  .bot {
    flex: 1;
    // height: calc(100vh - 100px);
    overflow: auto;
    display: flex;
    flex-direction: column;
    border-radius: 0 0 8px 8px;
    padding: 8px;
    background: #fff;
    .page-box {
      height: 50px;
      width: 100%;
      display: flex;
      align-items: flex-end;
    }
  }

  .totalBox{
    font-size: 18px;
    padding: 20px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .iconfont{
      color: red;
      font-weight: 600;
      font-size: 20px;
      padding-right: 4px;
    }
    .icon-xinxixiugaishenhe{
      color: #BF360C;
    }
    .icon-yiwancheng{
      color: #1B5E20;
    }
  }
  .nav-list {
    display: flex;
    padding: 0px 20px 0px;
    justify-content: center;
  }

  .nav-li {
    padding: 20px 20px 20px 8px;
    border-radius: 6px;
    width: 45%;
    margin: 0 2.5% 8px;
    background-image: url('@/static/img/nav-bg.png');
    background-size: cover;
    background-position: center;
    position: relative;
    z-index: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .iconfont {
      font-size: 40px;
    }
  }

  .nav-li::after {
    content: "";
    position: absolute;
    z-index: -1;
    background-color: inherit;
    width: 100%;
    height: 100%;
    left: 0;
    bottom: -10%;
    border-radius: 10px;
    opacity: 0.2;
    transform: scale(0.9, 0.9);
  }

  .nav-li.cur {
    color: #fff;
    background: rgb(94, 185, 94);
    box-shadow: 4px 4px 6px rgba(94, 185, 94, 0.4);
  }

  .nav-name {
    font-size: 16px;
    text-transform: Capitalize;
    margin-top: 0;
    padding: 10px 0 14px 20px;
    position: relative;
  }

  .nav-name::before {
    content: "";
    position: absolute;
    display: block;
    width: 40px;
    height: 6px;
    background: #fff;
    bottom: 0;
    right: -20px;
    opacity: 0.5;
  }

  .nav-name::after {
    content: "";
    position: absolute;
    display: block;
    width: 100px;
    height: 1px;
    background: #fff;
    bottom: 0;
    right: 0;
    opacity: 0.3;
  }

  .nav-name::first-letter {
    font-weight: bold;
    font-size: 24px;
    margin-right: 1px;
  }

  .title-header {
    display: flex;
    font-size: 28px;
    padding-bottom: 40rpx;
    align-items: center;
    justify-content: center;
    text-align: center;
    /* padding: 40rpx 0 0 0; */
    font-weight: bold;
  }

  .title-text {
    background-image: -webkit-linear-gradient(60deg, #388E3C, #26A69A);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .title-text-des {
    font-size: 34px;
    font-weight: 600;
    background-image: -webkit-linear-gradient(60deg, $u-grey-darken, $u-blue-grey-darken);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .bg-kuxuan1 {
    background-color: #BF360C80;
    color: #fff;
  }
  .doingActive{
    background-color: #BF360C;
  }
  .bg-kuxuan2 {
    background-color: #1B5E2080;
    color: #fff;
  }
  .doneActive{
    background-color: #1B5E20;
  }

  .qr-box {
    height: 200px;
    width: 200px;
    background: #f5f5f5;
    border-radius: 20px;
    display: flex;
    justify-content: center;
    align-items: center;

    .iconfont {
      font-size: 60px;
      color: $u-primary;
    }
  }
  :deep(.ua__thead-th){
    font-size: 16px !important;
    height: 40px;
    line-height: 40px;
  }
  :deep(.ua__tbody-td){
    font-size: 14px !important;
    height: 34px;
    line-height: 34px;
  }
  :deep(.u-tag__text){
    padding: 0 6px !important;
  }
  .slot-content{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    .iconfont{
      color:#00796b;
      font-size: 40rpx;
    }
    .title{
      font-size: 24rpx !important;
      padding-top: 18rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .icon-butongguo1{
      color:#D32F2F !important;
    }
    .content-boHui{
      flex-direction: row;
      align-items: center;
      justify-content: center;
      display: flex;
      .title{
        padding: 0;
        padding-left: 10px;
      }
    }
    .boHui-box{
      margin-top: 10rpx;
      width: 100%;
    }
  }
}
</style>